package com.woniu.airent.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.airent.dto.MyHouseDto;
import com.woniu.airent.dto.UserDto;
import com.woniu.airent.service.MyHouseService;
import com.woniu.airent.service.UserDtoService;
import com.woniu.airent.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/10/12
 * @since 1.0.0
 */
@Controller
@RequestMapping("houseCol")//收藏房源
public class MyHouseController {

    @Autowired
    MyHouseService myHouseService;

    @Autowired
    UserDtoService userDtoService;

    @RequestMapping("/houseColList")
    @ResponseBody
    public PageInfo<MyHouseDto> houseColList(String selectHouseResource,Integer colSelectByPrice,Integer colSelectByDate,Integer colPageNum,HttpServletRequest request){
        UserDto userDto = (UserDto) request.getSession().getAttribute("user");
        PageInfo<MyHouseDto> houses=myHouseService.findHouseColByPages(userDto.getUserId(),selectHouseResource,colSelectByPrice,colSelectByDate,colPageNum);
        return houses;
    }

    @RequestMapping("/removeHouseCol")
    @ResponseBody
    public int removeHouseCol(int colId){
        return myHouseService.removeHouseColByColId(colId);
    }

    @RequestMapping("/addHouseCol")
    @ResponseBody
    public int addHouseCol(int houseId, HttpServletRequest request){
        UserDto userDto=(UserDto) request.getSession().getAttribute("user");
        MyHouseDto myHouseDto=new MyHouseDto();
        myHouseDto.setUserId(userDto.getUserId());
        myHouseDto.setHouseId(houseId);
        return myHouseService.insertHouseCollection(myHouseDto);
    }

    @RequestMapping("isCollection")
    @ResponseBody
    public Integer isCollection(Integer houseId, HttpServletRequest request){
        UserDto userDto=(UserDto) request.getSession().getAttribute("user");
        if(userDto==null){
            return 2;
        };
        Integer result=myHouseService.findHouseIsCollection(userDto.getUserId(),houseId);
        if(result!=null ){
            return 1;
        };
        return  0;
    }

    @RequestMapping("userUpdate")
    public String userUpdate( UserDto userDto, HttpServletRequest request){
        String pwd=userDto.getUserPwd();
        String userName=userDto.getUserName();
        String newPwd=MD5Util.md5("MD5",pwd,userName,1024);
        userDto.setUserPwd(newPwd);
        userDtoService.userUpdate(userDto);
        return  "redirect:/userDto/logout";
    }

    @RequestMapping("/showUser")
    public String showUser(Integer userId,Map map){
        UserDto userDto=userDtoService.findUserByUserId(userId);
        map.put("user",userDto);
        return "view/user-person";
    }

    @RequestMapping("/userInfo")
    public String userInfo(Map maps , HttpServletRequest request){
        UserDto userDto = (UserDto) request.getSession().getAttribute("user");
        MyHouseDto myHouseDto=(MyHouseDto)request.getSession().getAttribute("cols");
        maps.put("user",userDto);
        maps.put("cols",myHouseDto);
        return "view/user";
    }
}

